package com.epfresh.api.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Html;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.text.method.KeyListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.supplier.R;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 17-7-10
 * @Description
 */
public class CommonInputItemThin extends RelativeLayout {
    private TextView tvTitle;
    private EditText edInput;
    private TextView tvInput;
    private ImageView ivFlag;
    private View vBottomLine;

    private int titleEms;
    private int contentColor;
    private int inputType;
    private String content;
    private String contentHint;
    private String title;
    private Drawable flagSrc;
    private boolean isEdit;
    private boolean isCleanable;
    private boolean isSingleLine;
    private boolean isEllipsize;
    private boolean isFlag;
    private boolean isNotNull;
    private int textGravity;
    private int maxLength;
    private int textPaddingTop;
    private int textPaddingRight;
    private int textPaddingLeft;
    private int textPaddingBottom;
    private int minLine;

    private boolean isLineVisible = true;

    private int textType;

    private int backgroundStatus;

    private String digits;
    int titleColor;
    int titleWidth;
    int titleHeight;

    public CommonInputItemThin(Context context) {
        this(context, null, 0);
    }

    public CommonInputItemThin(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public CommonInputItemThin(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CommonInputItem);

        maxLength = ta.getInt(R.styleable.CommonInputItem_content_text_length, -1);

        textType = ta.getInt(R.styleable.CommonInputItem_text_type,0);

        title = ta.getString(R.styleable.CommonInputItem_title_text);
        digits = ta.getString(R.styleable.CommonInputItem_content_ed_digits);
        content = ta.getString(R.styleable.CommonInputItem_content_text);
        contentHint = ta.getString(R.styleable.CommonInputItem_content_text_hint);
        title = ta.getString(R.styleable.CommonInputItem_title_text);
        titleEms = ta.getInt(R.styleable.CommonInputItem_title_ems, -1);
        flagSrc = ta.getDrawable(R.styleable.CommonInputItem_flag_src);
        isEdit = ta.getBoolean(R.styleable.CommonInputItem_type_edit, false);
        isFlag = ta.getBoolean(R.styleable.CommonInputItem_flag, true);
        isCleanable = ta.getBoolean(R.styleable.CommonInputItem_cleanable, false);
        isNotNull = ta.getBoolean(R.styleable.CommonInputItem_notnull, false);
        isEllipsize = ta.getBoolean(R.styleable.CommonInputItem_is_ellipsize, false);
        isSingleLine = ta.getBoolean(R.styleable.CommonInputItem_singleable, false);
        isLineVisible = ta.getBoolean(R.styleable.CommonInputItem_is_line, true);
        inputType = ta.getInt(R.styleable.CommonInputItem_inputType, -1);
        textGravity = ta.getInt(R.styleable.CommonInputItem_text_gravity, -1);
        minLine = ta.getInt(R.styleable.CommonInputItem_min_line, 1);
        textPaddingTop = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_text_padding_top, -1);
        textPaddingLeft = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_text_padding_left, -1);
        textPaddingRight = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_text_padding_right, -1);
        textPaddingBottom = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_text_padding_bottom, -1);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            contentColor = ta.getColor(R.styleable.CommonInputItem_content_text_color, getResources().getColor(R.color.font_color_def, null));

        } else {
            contentColor = ta.getColor(R.styleable.CommonInputItem_content_text_color, getResources().getColor(R.color.font_color_def));
        }
        titleColor = ta.getColor(R.styleable.CommonInputItem_title_color, getResources().getColor(R.color.font_color_def));
        titleWidth = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_title_width, -1);
        titleHeight = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_title_height, -1);

        ta.recycle();
    }

    public boolean isNotNull() {
        return isNotNull;
    }

    public void setNotNull(boolean notNull) {
        isNotNull = notNull;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        View child;
        if (isEdit) {
            child = View.inflate(getContext(), R.layout.common_input_item_thin, null);
            edInput = (EditText) child.findViewById(R.id.ed_input);

            setTextLayoutPadding(edInput);
            edInput.setTextColor(contentColor);
            edInput.setText(content);
            edInput.setHint(contentHint);
            edInput.setMinLines(minLine);

            if (digits != null) {
                edInput.setKeyListener(DigitsKeyListener.getInstance(digits));
            }

            if (maxLength > -1) {
                edInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
            }
            if (textGravity > -1) {
                edInput.setGravity(textGravity);
            }
        } else {
            child = View.inflate(getContext(), R.layout.common_text_item_thin, null);
            tvInput = (TextView) child.findViewById(R.id.tv_input);
            setTextLayoutPadding(tvInput);
            tvInput.setTextColor(contentColor);
            tvInput.setText(content);
            tvInput.setHint(contentHint);
            if (isSingleLine) {
                tvInput.setSingleLine();
            }
            if (isEllipsize) {
                tvInput.setSingleLine();
                tvInput.setEllipsize(TextUtils.TruncateAt.END);
            }
            if (maxLength > -1) {
                tvInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
            }
            if (textGravity > -1) {
                tvInput.setGravity(textGravity);
            }


        }
        ivFlag = (ImageView) child.findViewById(R.id.iv_flag);
        tvTitle = (TextView) child.findViewById(R.id.tv_title);
        vBottomLine = child.findViewById(R.id.v_bottom_line);

        if(isLineVisible){
            vBottomLine.setVisibility(VISIBLE);
        }else {
            vBottomLine.setVisibility(INVISIBLE);
        }

        if(textType!=0){
            tvTitle.setTypeface(null, textType);
            tvInput.setTypeface(null, textType);
        }

//        if (titleWidth == -1){
//            titleWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
//        }
//
//        if (titleHeight == -1){
//            titleHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
//
//        }
//
//        LayoutParams lp = new LayoutParams(titleWidth,titleHeight);
//        tvTitle.setLayoutParams(lp);

        if (isEdit && isCleanable) {
            updateCleanable();
            edInput.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    updateCleanable();
                }
            });

            ivFlag.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ivFlag.getVisibility() == View.VISIBLE)
                        edInput.setText("");
                }
            });

        }


        if (titleEms > 0) {
            tvTitle.setEms(titleEms);
        }
        if (flagSrc != null) {
            ivFlag.setImageDrawable(flagSrc);
        } else {
            ivFlag.setVisibility(View.VISIBLE);
            ivFlag.setClickable(false);
        }
        if (!isFlag) {
            ivFlag.setVisibility(View.GONE);
        }
        if (edInput != null && inputType > -1) {
            edInput.setInputType(inputType);
        }
        if (isNotNull) {
            title = title == null ? "" : title;
            String s = title + "<font color=\"#ff0000\"> *</font>";
            tvTitle.setText(Html.fromHtml(s));
        } else {
            tvTitle.setText(title);
        }
        addView(child);

        super.setOnClickListener(ocl);
    }

    private void setTextLayoutPadding(TextView edInput) {
        int paddingBottom = edInput.getPaddingBottom();
        int paddingTop = edInput.getPaddingTop();
        int paddingRight = edInput.getPaddingRight();
        int paddingLeft = edInput.getPaddingLeft();
        if (textPaddingLeft > 0) {
            paddingLeft = textPaddingLeft;
        }
        if (textPaddingTop > 0) {
            paddingTop = textPaddingTop;
        }
        if (textPaddingBottom > 0) {
            paddingBottom = textPaddingBottom;
        }
        if (textPaddingRight > 0) {
            paddingRight = textPaddingRight;
        }
        edInput.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    }

    public void setDigits(String digits) {
        if (digits != null) {
            edInput.setKeyListener(DigitsKeyListener.getInstance(digits));
        }
    }

    public String getTitleText() {
        return tvTitle.getText().toString();
    }

    public String getContentText() {
        if (edInput != null) {
            return edInput.getText().toString();
        } else {
            return tvInput.getText().toString();
        }
    }

    public void setSelectionLast() {
        if (edInput != null) {
            edInput.setSelection(edInput.length());
        }
    }

    public void setContentText(String text) {
        if (edInput != null) {
            edInput.setText(text);
        } else {
            tvInput.setText(text);
        }
    }

    public void setContentText(CharSequence text) {
        if (edInput != null) {
            edInput.setText(text);
        } else {
            tvInput.setText(text);
        }
    }

    public void setContentText(Spanned text) {
        if (edInput != null) {
            edInput.setText(text);
        } else {
            tvInput.setText(text);
        }
    }

    public void setContentHintText(String text) {
        if (edInput != null) {
            edInput.setHint(text);
        } else {
            tvInput.setHint(text);
        }
    }

    public ImageView getIvFlag() {
        return ivFlag;
    }

    public void setIvFlag(ImageView ivFlag) {
        this.ivFlag = ivFlag;
    }

    public EditText getEdInput() {
        return edInput;
    }

    public void setTitleText(String title) {
        if (isNotNull) {
            title = title == null ? "" : title;
            String s = title + "<font color=\"#ff0000\"> *</font>";
            tvTitle.setText(Html.fromHtml(s));
        } else {
            tvTitle.setText(title);
        }
    }

    public void setSelect(String title, boolean isSelect) {

        String color = "#959595";

        if (isSelect) {

            color = "#00b7ee";
        }
        if (edInput != null) {
            edInput.setHint(title);
            edInput.setHintTextColor(Color.parseColor(color));
        } else {
            tvInput.setHint(title);
            tvInput.setHintTextColor(Color.parseColor(color));
        }
    }

    public void setSelect(boolean isSelect) {
        String title = "";

        String color = "#959595";

        if (isSelect) {
            title = getContext().getString(R.string.already_setting);
            color = "#00b7ee";
        } else {
            title = getContext().getString(R.string.no_setting);
        }
        if (edInput != null) {
            edInput.setHint(title);
            edInput.setHintTextColor(Color.parseColor(color));
        } else {
            tvInput.setHint(title);
            tvInput.setHintTextColor(Color.parseColor(color));
        }
    }

    public TextView getContent() {
        if (edInput != null) {
            return edInput;
        } else {
            return tvInput;
        }
    }

    public void setText(String text, int type) {
        String color = "#959595";

        if (type > 0) {
            color = "#00b7ee";
        } else if (type < 0) {//未通过
            color = "#ff0000";
        } else {

        }
        if (edInput != null) {
            edInput.setHint(text);
            edInput.setHintTextColor(Color.parseColor(color));
        } else {
            tvInput.setHint(text);
            tvInput.setHintTextColor(Color.parseColor(color));
        }
    }

    boolean isNotClean;

    public void updateCleanable() {
        if (edInput != null && edInput.length() > 0 && !isNotClean) {
            ivFlag.setVisibility(View.VISIBLE);
            ivFlag.setClickable(true);
        } else if (edInput != null) {
            ivFlag.setVisibility(View.INVISIBLE);
            ivFlag.setClickable(false);
        }
    }

    public void setTextEnabled(boolean isEnable) {
        if (edInput != null) {
            edInput.setFocusable(isEnable);
            edInput.setFocusableInTouchMode(isEnable);
            if (isEnable) {
//                edInput.requestFocus();
            }


        }
        if (tvInput != null) {
            tvInput.setEnabled(isEnable);
            this.setEnabled(isEnable);
        }
        isNotClean = !isEnable;
        if (!isEnable)
            ivFlag.setVisibility(View.GONE);
    }

    public void setHintEnable(boolean isEnable) {
        if (edInput != null) {
            edInput.setFocusable(isEnable);
            edInput.setFocusableInTouchMode(isEnable);
            if (isEnable) {
                edInput.setTextColor(Color.parseColor("#434343"));
            } else {
                edInput.setTextColor(Color.parseColor("#b3b3b3"));
            }
        }
        if (tvInput != null) {
            tvInput.setEnabled(isEnable);
            this.setEnabled(isEnable);
        }

    }

    public void setCanEdit(boolean isCanEdit) {
        if (!isCanEdit) {
            getChildAt(0).setBackgroundColor(Color.parseColor("#f5f5f5"));
            if (tvInput != null) {
                tvInput.setEnabled(isCanEdit);
                tvInput.setTextColor(Color.parseColor("#959595"));
            }
            if (edInput != null) {
                edInput.setTextColor(Color.parseColor("#959595"));
                edInput.setEnabled(isCanEdit);
            }
            setEnabled(isCanEdit);
            ivFlag.setVisibility(INVISIBLE);
        } else {
            getChildAt(0).setBackgroundColor(Color.parseColor("#ffffff"));
            if (tvInput != null) {
                tvInput.setEnabled(isCanEdit);
                tvInput.setTextColor(Color.parseColor("#434343"));
            }
            if (edInput != null) {
                edInput.setTextColor(Color.parseColor("#434343"));
                edInput.setEnabled(isCanEdit);
            }
            setEnabled(isCanEdit);
            ivFlag.setVisibility(INVISIBLE);
        }
    }

    OnClickListener l;
    OnClickListener onCanNotEdit;
    boolean isCanEdit = true;
    OnClickListener ocl = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isCanEdit) {
                if (l != null) {
                    l.onClick(v);
                }
            } else {
                if (onCanNotEdit != null) {
                    onCanNotEdit.onClick(v);
                }
            }
        }
    };

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        this.l = l;
    }

    public OnClickListener getOnCanNotEdit() {
        return onCanNotEdit;
    }

    public void setOnCanNotEdit(OnClickListener onCanNotEdit) {
        this.onCanNotEdit = onCanNotEdit;
    }

    KeyListener keyListener;

    public void setEditable(boolean isCanEdit) {
        this.isCanEdit = isCanEdit;
        if (!isCanEdit) {
            ivFlag.setVisibility(INVISIBLE);
            ivFlag.setClickable(false);
//            getChildAt(0).setBackgroundColor(Color.parseColor("#f5f5f5"));
            if (tvInput != null) {
                tvInput.setEnabled(isCanEdit);
//                edInput.setBackgroundColor(0xffffff);
//                tvInput.setTextColor(Color.parseColor("#959595"));
            }
            if (edInput != null) {
//                edInput.setTextColor(Color.parseColor("#959595"));
//                edInput.setEnabled(isCanEdit);
//                edInput.setFocusable(false);
//                edInput.setFocusableInTouchMode(false);

                KeyListener kl = edInput.getKeyListener();
                if (kl != null) {
                    keyListener = kl;
                }
                edInput.setKeyListener(null);
                edInput.setOnTouchListener(new EtOnTouchListener());

                edInput.setOnClickListener(onCanNotEdit);

//                edInput.setBackgroundColor(0xffffff);
            }
//            setEnabled(isCanEdit);

        } else {
            getChildAt(0).setBackgroundColor(Color.parseColor("#ffffff"));
            if (tvInput != null) {
                tvInput.setEnabled(isCanEdit);
                tvInput.setTextColor(Color.parseColor("#434343"));
            }
            if (edInput != null) {
                KeyListener kl = edInput.getKeyListener();
                if (kl == null) {
                    edInput.setKeyListener(keyListener);
                }

                edInput.setTextColor(Color.parseColor("#434343"));
//                edInput.setEnabled(isCanEdit);
//                edInput.setFocusableInTouchMode(true);
//
//                edInput.setFocusable(true);
                edInput.setOnClickListener(null);
//                edInput.requestFocus();
                edInput.setOnTouchListener(null);

                edInput.setKeyListener(new DigitsKeyListener());

            }
//            setEnabled(isCanEdit);
            ivFlag.setVisibility(VISIBLE);
            ivFlag.setClickable(true);
        }
    }

    public void showFlag(boolean show) {
        if (show) {
            ivFlag.setVisibility(View.VISIBLE);
        } else {
            ivFlag.setVisibility(View.INVISIBLE);
        }

    }

    public static int STATUS_DIS_ENABLE = 0x11;
    public static int STATUS_NORMAL = 0x00;
    public int status = 0x00;

    public void setBgStatus(int status) {
        this.status = status;
        if (status == STATUS_DIS_ENABLE) {//不可点击
            getChildAt(0).setBackgroundColor(Color.parseColor("#f5f5f5"));
            if (tvInput != null) {

                tvInput.setTextColor(Color.parseColor("#959595"));
            }
            if (edInput != null) {
                edInput.setTextColor(Color.parseColor("#959595"));

            }


            ivFlag.setVisibility(INVISIBLE);

        } else if (status == STATUS_NORMAL) {//正常状态
            getChildAt(0).setBackgroundColor(Color.parseColor("#ffffff"));
            if (tvInput != null) {

                tvInput.setTextColor(Color.parseColor("#434343"));
            }
            if (edInput != null) {
                edInput.setTextColor(Color.parseColor("#434343"));

            }

            ivFlag.setVisibility(VISIBLE);

        }
    }

    public void setStatusEnable(boolean isEnable) {
        if (isEnable) {
            setBgStatus(STATUS_NORMAL);
        } else {
            setBgStatus(STATUS_DIS_ENABLE);
        }
    }

    public boolean isStatusEnable() {
        return status == STATUS_DIS_ENABLE ? false : true;
    }

    public boolean isContentEmpty() {
        if (isEdit) {
            return TextUtils.isEmpty(edInput.getText());
        } else {
            return TextUtils.isEmpty(tvInput.getText());
        }
    }

    class EtOnTouchListener implements OnTouchListener {

        int touch_flag = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            touch_flag++;
            if (touch_flag == 2) {
                touch_flag = 0;

            }
            return false;
        }
    }
}
